package http

import (
	"net/http"

	"gitee.com/hlwqds/go-my-cache-server/cache"
	"gitee.com/hlwqds/go-my-cache-server/cluster"
)

type Server struct {
	cache.Cache
	cluster.Node
}

func New(c cache.Cache, node cluster.Node) *Server {
	return &Server{c, node}
}

func (s *Server) Listen() {
	http.Handle("/cache/", s.cacheHandler())
	http.Handle("/cache/status", s.statusHandler())
	http.Handle("/cache/cluster", s.clusterHandler())
	http.Handle("/cache/rebalance", s.rebalanceHandler())

	err := http.ListenAndServe(":12345", nil)
	if err != nil {
		panic(err)
	}
}

func (s *Server) cacheHandler() http.Handler {
	return &cacheHandler{s}
}

func (s *Server) statusHandler() http.Handler {
	return &statusHandler{s}
}

func (s *Server) clusterHandler() http.Handler {
	return &clusterHandler{s}
}

func (s *Server) rebalanceHandler() http.Handler {
	return &rebalanceHandler{s}
}
